package ljl.codetop;

import commons.TreeNode;

public class _543_diameter_of_binary_tree {

    /**
     *           1
     *          / \
     *         2   3
     *        / \
     *       4   5
     */
    static class have_a_try {
        // 我记着是：计算左右孩子层高；同时更新最大值

        int max;
        public int diameterOfBinaryTree(TreeNode root) {
            height(root);
            return max;
        }
        int height(TreeNode node) {
            if (node == null) return 0;
            int l = height(node.left);
            int r = height(node.right);
            max = Math.max(max, l + r);
            return Math.max(l, r) + 1;
        }
    }
}
